package com.bwei.topnews;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;

import com.bwei.topnews.bean.NewsTitleRoot;
import com.bwei.topnews.base.BaseData;
import com.bwei.topnews.utils.CommonUtils;
import com.bwei.topnews.utils.LogUtils;
import com.bwei.topnews.utils.TitileUtils;
import com.bwei.topnews.utils.URLUtils;
import com.google.gson.Gson;

public class LaunchActivity extends AppCompatActivity {

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            // 跳转
            startActivity(new Intent(LaunchActivity.this, MainActivity.class));
            // 关闭
            handler.removeCallbacksAndMessages(null);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launch);
        //3秒后发送消息
        handler.sendEmptyMessageDelayed(0, 3000);
        // 请求标题
        BaseData baseData = new BaseData() {
            @Override
            public void setResultData(String data) {
                // 判断文件中是否有数据，有的话就不存
                if (!TextUtils.isEmpty(CommonUtils.getSp("myChannel"))) {
                    return;
                }
                Gson gson = new Gson();
                NewsTitleRoot newsTitleRoot = gson.fromJson(data, NewsTitleRoot.class);
                StringBuilder stringBuilder_my = new StringBuilder();
                StringBuilder stringBuilder_more = new StringBuilder();
                stringBuilder_my.append("推荐:,");
                for (int i = 0; i < 15; i++) {
                    String category = newsTitleRoot.getData().getData().get(i).getCategory();
                    String name = newsTitleRoot.getData().getData().get(i).getName();
                    stringBuilder_my.append(name + ":" + category + ",");
                }
                for (int i = 15; i < newsTitleRoot.getData().getData().size(); i++) {
                    String category = newsTitleRoot.getData().getData().get(i).getCategory();
                    String name = newsTitleRoot.getData().getData().get(i).getName();
                    stringBuilder_more.append(name + ":" + category + ",");

                }
                CommonUtils.saveSp("myChannel", stringBuilder_my.toString());
                CommonUtils.saveSp("moreChannel", stringBuilder_more.toString());
            }

            @Override
            public void setFailResult(int error_Net) {
            }
        };
        baseData.getData(URLUtils.URL_TITLE_PATH, BaseData.LONGTTIME);

        BaseData baseData1 = new BaseData() {
            @Override
            public void setResultData(String data) {
            }

            @Override
            public void setFailResult(int error_Net) {
            }
        };
        baseData1.getData(URLUtils.URL_CATEGROY_PATH, BaseData.LONGTTIME);

        LogUtils.i("TAG", URLUtils.URL_CATEGROY_PATH + "+++++++++++");

        BaseData baseData2 = new BaseData() {
            @Override
            public void setResultData(String data) {

            }

            @Override
            public void setFailResult(int error_Net) {

            }
        };
        baseData2.getData(URLUtils.URL_TITLE_PATH + "news_hot", BaseData.LONGTTIME);
    }
}
